package code.jzh.compiler.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Constants {
	/*public static final char star = '×';
	public static final char union = '或';
	public static final char concatenation = '。';
	public static final char left_parentheses = '（';
	public static final char right_parentheses = '）';
	public static final char left_bracket = '【';
	public static final char right_bracket = '】';*/
	public static final char star = '*';
	public static final char union = '|';
	public static final char concatenation = '+';
	public static final char left_parentheses = '(';
	public static final char right_parentheses = ')';
	public static final char left_bracket = '[';
	public static final char right_bracket = ']';
	public static final char escape = '\\';
	public static final char quote = '"';
	
	public static final char epsilon = 'ε';
	
	public static Map<Character, Integer> precedenceMap = new HashMap<Character, Integer>();
	static {
		precedenceMap.put(left_parentheses, 1);
		precedenceMap.put(star, 2);
		precedenceMap.put(concatenation, 3);
		precedenceMap.put(union, 4);
	}
	
	public static int getPrecedence(char c) {
		return precedenceMap.get(c);
	}
	
	public static List<Character> charList = new ArrayList<Character>();
	static {
		for(char c=0; c<=127; c++) {
			charList.add(c);
		}
	}
	
	//s比t优先级高(数值上越小，优先级越高) 返回true
	public static boolean precedenceHigher(char s, char t) {
		return getPrecedence(s)<getPrecedence(t);
	}
	
	public static boolean precedenceEqual(char s, char t) {
		return getPrecedence(s)==getPrecedence(t);
	}
	
	public static boolean precedenceLower(char s, char t) {
		return getPrecedence(s)>getPrecedence(t);
	}
	
	public static boolean isOperator(char c) {
		if ( c==star || c==union || c==left_parentheses || c==right_parentheses || c==concatenation)
			return true;
		return false;
	}
	
	public static boolean needEscape(char c) {
		if(c==escape || c==left_bracket || c==right_bracket)
			return true;
		return isOperator(c);
	}
}
